<template>
  <div class="user-detail-container">
    <el-page-header @back="$router.back()" class="page-header">
      <template #icon>
        <el-icon class="back-icon"><ArrowLeft /></el-icon>
      </template>
      <template #content>
        <span class="page-title">用户详情</span>
      </template>
    </el-page-header>

    <div v-loading="loading" class="content-wrapper">
      <!-- 用户信息卡片 -->
      <el-card class="user-info-card" shadow="hover">
        <div class="user-header">
          <el-avatar :size="80" class="user-avatar">
            {{ userDetail.nickName ? userDetail.nickName.charAt(0) : userDetail.username?.charAt(0) || 'U' }}
          </el-avatar>
          <div class="user-basic">
            <div class="username-row">
              <h2 class="username">{{ userDetail.username }}</h2>
              <el-tag :type="Number(userDetail.status) === 1 ? 'success' : 'danger'" size="large">
                {{ Number(userDetail.status) === 1 ? '正常' : '禁用' }}
              </el-tag>
            </div>
            <div class="user-meta">
              <span class="meta-item">
                <el-icon><User /></el-icon>
                ID: {{ userDetail.user_id }}
              </span>
              <el-divider direction="vertical" />
              <span class="meta-item">
                <el-icon><Star /></el-icon>
                {{ userDetail.nickName || '未设置昵称' }}
              </span>
              <el-divider direction="vertical" />
              <el-tag :type="userDetail.role === 'admin' ? 'danger' : 'primary'" effect="plain">
                <el-icon><UserFilled /></el-icon>
                {{ userDetail.role === 'admin' ? '管理员' : '普通用户' }}
              </el-tag>
            </div>
          </div>
        </div>

        <el-divider />

        <el-row :gutter="20" class="time-info">
          <el-col :span="12">
            <div class="info-item">
              <el-icon class="info-icon" color="#409eff"><Clock /></el-icon>
              <div class="info-content">
                <div class="info-label">注册时间</div>
                <div class="info-value">{{ userDetail.createdAt || '未知' }}</div>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="info-item">
              <el-icon class="info-icon" color="#67c23a"><Calendar /></el-icon>
              <div class="info-content">
                <div class="info-label">最后登录</div>
                <div class="info-value">{{ userDetail.lastLoginTime || '未登录' }}</div>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-card>

      <!-- 数据统计 -->
      <div class="section-title">
        <el-icon><DataAnalysis /></el-icon>
        <span>数据统计</span>
      </div>

      <el-row :gutter="20" class="stats-row">
        <el-col :xs="24" :sm="12" :md="8">
          <div class="stat-card" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
            <div class="stat-icon">
              <el-icon><ShoppingBag /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ userDetail.productCount || 0 }}</div>
              <div class="stat-label">发布商品</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8">
          <div class="stat-card" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);">
            <div class="stat-icon">
              <el-icon><ShoppingCart /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ userDetail.buyOrderCount || 0 }}</div>
              <div class="stat-label">购买订单</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8">
          <div class="stat-card" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
            <div class="stat-icon">
              <el-icon><Sell /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ userDetail.sellOrderCount || 0 }}</div>
              <div class="stat-label">销售订单</div>
            </div>
          </div>
        </el-col>
      </el-row>

      <el-row :gutter="20" class="stats-row">
        <el-col :xs="24" :sm="12" :md="8">
          <div class="stat-card" style="background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);">
            <div class="stat-icon">
              <el-icon><Document /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ userDetail.postCount || 0 }}</div>
              <div class="stat-label">发帖数</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8">
          <div class="stat-card" style="background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);">
            <div class="stat-icon">
              <el-icon><ChatDotRound /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ userDetail.commentCount || 0 }}</div>
              <div class="stat-label">评论数</div>
            </div>
          </div>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8">
          <div class="stat-card" style="background: linear-gradient(135deg, #30cfd0 0%, #330867 100%);">
            <div class="stat-icon">
              <el-icon><StarFilled /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ userDetail.favoriteCount || 0 }}</div>
              <div class="stat-label">收藏数</div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  ArrowLeft,
  User,
  UserFilled,
  Star,
  StarFilled,
  Clock,
  Calendar,
  DataAnalysis,
  ShoppingBag,
  ShoppingCart,
  Sell,
  Document,
  ChatDotRound
} from '@element-plus/icons-vue'
import request from '@/utils/request'

const route = useRoute()
const loading = ref(false)
const userDetail = ref({})

const getUserDetail = async () => {
  loading.value = true
  try {
    const userId = route.params.id
    const response = await request.get(`/admin/user/detail/${userId}`)
    if (response.code === 0) {
      userDetail.value = response.data
    } else {
      ElMessage.error(response.message || '获取用户详情失败')
    }
  } catch (error) {
    console.error('获取用户详情失败', error)
    ElMessage.error('获取用户详情失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  getUserDetail()
})
</script>

<style scoped>
.user-detail-container {
  padding: 24px;
  background: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.page-header {
  background: #fff;
  padding: 16px 24px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.back-icon {
  font-size: 18px;
  color: #409eff;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.content-wrapper {
  max-width: 1200px;
  margin: 0 auto;
}

/* 用户信息卡片 */
.user-info-card {
  margin-bottom: 24px;
  border-radius: 12px;
  overflow: hidden;
}

.user-header {
  display: flex;
  align-items: center;
  gap: 24px;
}

.user-avatar {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  font-size: 32px;
  font-weight: bold;
  flex-shrink: 0;
}

.user-basic {
  flex: 1;
}

.username-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.username {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
}

.user-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  color: #606266;
  font-size: 14px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.meta-item .el-icon {
  font-size: 16px;
}

/* 时间信息 */
.time-info {
  margin-top: 16px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  background: #f5f7fa;
  border-radius: 8px;
  transition: all 0.3s;
}

.info-item:hover {
  background: #ecf5ff;
  transform: translateY(-2px);
}

.info-icon {
  font-size: 32px;
}

.info-content {
  flex: 1;
}

.info-label {
  font-size: 13px;
  color: #909399;
  margin-bottom: 4px;
}

.info-value {
  font-size: 15px;
  color: #303133;
  font-weight: 500;
}

/* 章节标题 */
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
  margin: 32px 0 16px;
  padding-left: 12px;
  border-left: 4px solid #409eff;
}

.section-title .el-icon {
  font-size: 20px;
  color: #409eff;
}

/* 统计卡片 */
.stats-row {
  margin-bottom: 20px;
}

.stat-card {
  padding: 24px;
  border-radius: 12px;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: all 0.3s;
  cursor: pointer;
  height: 110px;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
}

.stat-icon {
  width: 60px;
  height: 60px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  flex-shrink: 0;
}

.stat-content {
  flex: 1;
}

.stat-value {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 4px;
  line-height: 1;
}

.stat-label {
  font-size: 14px;
  opacity: 0.9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .user-header {
    flex-direction: column;
    text-align: center;
  }

  .username-row {
    justify-content: center;
  }

  .user-meta {
    flex-wrap: wrap;
    justify-content: center;
  }

  .stat-card {
    height: auto;
    min-height: 100px;
  }
}
</style>

